package com.loongtech.bi.action.monitor;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.system.EntityMonitorUserConfig;
import com.loongtech.bi.manager.system.*;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * Author: yuri
 * Description: 告警用户模块
 * Date: 15:44 2017/5/15
 */
@RestController
@Scope("prototype")
@RequestMapping("/monitoruser")
public class MonitorUserConfigAction {
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private BiMonitorUserConfigManager biMonitorUserConfigManager;
    @Resource
    private BiMonitorGroupConfigManager biMonitorGroupConfigManager;
    @Resource
    private BiMonitorUserGroupManager biMonitorUserGroupManager;
    @Resource
    private SyncInfoLogManager syncInfoLogManager;



    /**
     * 查询
     */
    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> searchByCriteria(EntityMonitorUserConfig entityMonitorUserConfig) {
        List<EntityMonitorUserConfig> list;
        try {
            list = biMonitorUserConfigManager.searchByCriteria(entityMonitorUserConfig);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    /**
     * 增加
     */
    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> addUser(EntityMonitorUserConfig entityMonitorUserConfig) {
        if (Utils.isEmptyStr(entityMonitorUserConfig.getUserName())) {
            return new ResultData<>(RetCode.eEmptyUserNameError.getErrorcode(), RetCode.eEmptyUserNameError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getWeChat())) {
            return new ResultData<>(RetCode.eEmptyWeChatError.getErrorcode(), RetCode.eEmptyWeChatError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getPhoneNum())) {
            return new ResultData<>(RetCode.eEmptyPhoneNumError.getErrorcode(), RetCode.eEmptyPhoneNumError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getVoiceNum())) {
            return new ResultData<>(RetCode.eEmptyVoiceNumError.getErrorcode(), RetCode.eEmptyVoiceNumError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getEmail())) {
            return new ResultData<>(RetCode.eEmptyEmailError.getErrorcode(), RetCode.eEmptyEmailError.getErrorMsg());
        }
        try {
            biMonitorUserConfigManager.addUser(entityMonitorUserConfig);

            StringBuilder insertSQL = new StringBuilder("insert into bi_moniter_userconfig(userName,email,phoneNum,voiceNum,weChat,creatTime) values");
            insertSQL.append("(").append("'").append(entityMonitorUserConfig.getUserName()).append("','").append(entityMonitorUserConfig.getEmail()).append("','").append(entityMonitorUserConfig.getPhoneNum()).append("','").
                    append(entityMonitorUserConfig.getVoiceNum()).append("','"). append(entityMonitorUserConfig.getWeChat()).append("',").append(Time.currentTime()).append(")");
            Set<String> gameId = SysProjectManager.projectIdNameMap.keySet();
            for (String gid : gameId) {
                syncInfoLogManager.add(gid, insertSQL.toString());
            }

            sysLogManager.addLog(session.getUserName(), "增加告警用户成功:" + entityMonitorUserConfig.getUserName());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "delete.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> delUser(@RequestParam(value = "id") Integer id) {
        try {
            EntityMonitorUserConfig cur = biMonitorUserConfigManager.get(id);
            if (cur != null) {
                biMonitorUserConfigManager.delUser(id);
                biMonitorUserGroupManager.excuteUpdateByHql("delete from EntityMonitorUserGroup where userId = ?", id);

                Set<String> gameId = SysProjectManager.projectIdNameMap.keySet();
                for (String gid : gameId) {
                    syncInfoLogManager.add(gid, "delete from bi_moniter_userconfig where id = " + id );
                }
                sysLogManager.addLog(session.getUserName(), "告警用户删除成功");
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNotExistUser.getErrorcode(), RetCode.eNotExistUser.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modifiyUser(EntityMonitorUserConfig entityMonitorUserConfig) {
        if (Utils.isEmptyStr(entityMonitorUserConfig.getUserName())) {
            return new ResultData<>(RetCode.eEmptyUserNameError.getErrorcode(), RetCode.eEmptyUserNameError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getWeChat())) {
            return new ResultData<>(RetCode.eEmptyWeChatError.getErrorcode(), RetCode.eEmptyWeChatError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getPhoneNum())) {
            return new ResultData<>(RetCode.eEmptyPhoneNumError.getErrorcode(), RetCode.eEmptyPhoneNumError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getVoiceNum())) {
            return new ResultData<>(RetCode.eEmptyVoiceNumError.getErrorcode(), RetCode.eEmptyVoiceNumError.getErrorMsg());
        }
        if (Utils.isEmptyStr(entityMonitorUserConfig.getEmail())) {
            return new ResultData<>(RetCode.eEmptyEmailError.getErrorcode(), RetCode.eEmptyEmailError.getErrorMsg());
        }
        try {
            EntityMonitorUserConfig cur = biMonitorUserConfigManager.get(entityMonitorUserConfig.getId());
            if (cur != null) {
                entityMonitorUserConfig.setCreatTime(cur.getCreatTime());
                biMonitorUserConfigManager.modifiyUser(entityMonitorUserConfig);

                StringBuilder updateSQL = new StringBuilder("update bi_moniter_userconfig set userName = ");
                updateSQL.append("'").append(entityMonitorUserConfig.getUserName()).append("',email = '").append(entityMonitorUserConfig.getEmail()).append("',phoneNum = '").append(entityMonitorUserConfig.getPhoneNum()).
                        append("',voiceNum = '").append(entityMonitorUserConfig.getVoiceNum()).append("',weChat = '").append(entityMonitorUserConfig.getWeChat()).append("',creatTime = ").append(Time.currentTime()).append(" where  id = ").append(entityMonitorUserConfig.getId());
                Set<String> gameId = SysProjectManager.projectIdNameMap.keySet();
                for (String gid : gameId) {
                    syncInfoLogManager.add(gid, updateSQL.toString());
                }

                sysLogManager.addLog(session.getUserName(), "告警用户修改成功");
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNotExistUser.getErrorcode(), RetCode.eNotExistUser.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}
